package com.example.mediaplayerclassroom;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    SurfaceView mSurfaceView;
    Button mPlayButton;
    MediaPlayer mPlayer;
    TextView mProcessText;
    SeekBar mProcess;
    TextView mVideoTotal;
    Handler mHandler = new Handler();
    Runnable mRunable = new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProcess.setProgress(mPlayer.getCurrentPosition());
                }
            });
            mHandler.postDelayed(mRunable,1000);
        }

    };

//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        if (requestCode == 1) {
//            if (grantResults[0] == permissions)
//        }
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        //mediastore的使用
        int i = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);  //检查写权限
        if (i == -1) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }

        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
//        cursor.g
        String[] names = cursor.getColumnNames();
//        for (String name : names) {
//            Log.i("=======", "name" + name);
//        }
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String cursorString = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                Log.i("=======", "string" + cursorString);
            }
        }

        //视频播放
        mPlayer = MediaPlayer.create(this, R.raw.video);
        mProcess.setMax(mPlayer.getDuration());
        mProcess.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    mPlayer.seekTo(i);
                }

//                mProcess.setProgress(i);
//                int second = i / 1000;
//                int minutes = second / 60;
//                int seconds = second - minutes * 60;
//                mProcessText.setText(Integer.toString(minutes)+":"+Integer.toString(seconds));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mPlayer.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mPlayer.start();
            }
        });
        mPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SurfaceHolder holder = mSurfaceView.getHolder();
                mHandler.postDelayed(mRunable, 1000);
                mPlayer.setDisplay(holder);
                if (mPlayer.isPlaying()) {
                    mHandler.postDelayed(mRunable, 1000);
                    mPlayer.pause();
                    mPlayButton.setText("播放");
                } else {
                    mHandler.postDelayed(mRunable, 1000);
                    mPlayer.start();
                    mPlayButton.setText("暂停");
                }
//                mPlayer.start();
            }
        });

    }

    private void initView() {
        mSurfaceView = findViewById(R.id.video_view);
        mPlayButton = findViewById(R.id.play_button);
        mProcess = findViewById(R.id.video_seekbar);
        mProcessText = findViewById(R.id.default_seek);
        mVideoTotal = findViewById(R.id.video_total);
    }
}